<?php
/**
 * 生成站内，站外邀请链接，验证入站链接合法性
 * 
 * @author qingyun
 */

class OAInviteUrl
{
    const APP_KEY_SIG = 'app_key_sig_str';
    const INVITE_ENCRYPTION_KEY = 'eBcAip4aqW8618nU';
    const INVITE_URL_NAMESPACE = 'sdo_sig';
    
    /**
     * 从一个数组和一个密钥中生成签名
     * 
     * @param Array $sourceArr  源数组
     * @param String $secretKey 密钥
     * @return String 生成的签名
     */
    public static function generateSignature($sourceArr, $secretKey)
    {
        $signature = '';
        
        ksort($sourceArr);
        foreach ($sourceArr as $key => $val)
        {
            $signature .= "$key=$val";
        }
        $signature .= $secretKey;
        
        return md5($signature);
    }
    
    /**
     * 为param数组各键值加上sdo_sig前缀和sdo_sig密钥
     * 
     * @param Array $destArr
     * @param String $secretKey
     */
    public static function getSignaturedParam(&$destArr, $secretKey)
    {
        if (!isset($destArr))
        {
            $destArr = array();
        }
        
        $temp = $destArr;
        $destArr = array();
        
        $signature = self::generateSignature($temp, $secretKey);
        $sigPrefix = self::INVITE_URL_NAMESPACE . '_';
        
        foreach ($temp as $key => $val)
        {
            $destArr[$sigPrefix . $key] = $val;
        }
        
        $destArr[self::INVITE_URL_NAMESPACE] = $signature;
    }
    
    /**
     * 取得站内邀请链接
     * 
     * @param Array $srcArr array('from'=> ,'app_key'=>, ...);
     * @param String $secret self::APP_KEY_SIG
     */
    /*
    public static function getInnerInviteUrl($srcArr, $secret)
    {
        self::getInviteSignaturedParam($srcArr, $secret);
        
        $url = 'http://apps.sdo.com/playapp?' . http_build_query($srcArr);
        
        return $url;
    }
    */
    
    /**
     * 获取站外邀请链接
     * 
     * @param Array $srcArr array('from'=> ,'app_key'=>, ...);
     * @param String $secret self::APP_KEY_SIG
     */
    /*
    public static function getOuterInviteUrl($srcArr, $secret)
    {
        $reg_params['return_url'] = self::getInnerInviteUrl($srcArr, $secret);
        $serialized_reg_params = serialize($reg_params);
        $token = md5($serialized_reg_params . self::INVITE_ENCRYPTION_KEY);
        $srcArr['token'] = $token;
        
        //$url = 'http://i.sdo.com/invite/index.php?user_id=xxx&code=xxxxxxx';
        $url = 'http://i.sdo.com/invite/index.php?' . http_build_query($srcArr);
        
        return $url;
    }
    */
    
    /**
     * 获取应用的邀请链接
     * 
     * @param integer $appId
     * @param integer $inviterUid 邀请者uid
     * @param boolean $outer 是否是站外邀请
     * @return string
     */
    public static function getAppInviteUrl($appId, $inviterUid, $outer = FALSE)
    {
        $appDB = new OpAppModel();
        
        $app = $appDB->getAppById($appId);
        if (!$app) {
            return '';    
        }
        
        $baseUrl = 'http://apps.sdo.com/' . $app['canvas_page_url'];
        
        $baseUrl .= '?from=' . $inviterUid;
        
        if ($outer) {
            $baseUrl .= '&new=1';
        }
        
        return $baseUrl;
    }
    
    /**
     * 应用新被授权时通知应用开放方
     */
    public static function notifySpAppAuth($baseUrl, $params)
    {
        $url = $baseUrl;
        
        $reqMethod = 'http';
        if (stripos($baseUrl, 'https') === 0)
        {
            $reqMethod = 'https';
        }
        if (strpos($baseUrl, '?') !== FALSE)
        {
            $url .= '?';
        }
        
        self::getSignaturedParam($params, self::INVITE_ENCRYPTION_KEY);
        
        $url .= http_build_query($params);
            
        return Request::getContent($url, $reqMethod);        
    }
}